package com.industrial.premu.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuDifferentiationPolicyItem;
import com.industrial.premu.mapper.PreMuDifferentiationPolicyItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.industrial.premu.mapper.PreMuDifferentiationPolicyMapper;
import com.industrial.premu.domain.PreMuDifferentiationPolicy;
import com.industrial.premu.service.IPreMuDifferentiationPolicyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 差别化政策管理Service业务层处理
 *
 * @author SangGuangCan
 * @date 2024-02-27
 */
@Service
public class PreMuDifferentiationPolicyServiceImpl implements IPreMuDifferentiationPolicyService
{
    @Autowired
    private PreMuDifferentiationPolicyMapper preMuDifferentiationPolicyMapper;

    @Autowired
    private PreMuDifferentiationPolicyItemMapper preMuDifferentiationPolicyItemMapper;

    @Value("${file.pre}")
    private String filePre;

    /**
     * 查询差别化政策管理
     *
     * @param baseId 差别化政策管理主键
     * @return 差别化政策管理
     */
    @Override
    public PreMuDifferentiationPolicy selectPreMuDifferentiationPolicyByBaseId(String baseId)
    {
        return preMuDifferentiationPolicyMapper.selectPreMuDifferentiationPolicyByBaseId(baseId);
    }

    /**
     * 查询差别化政策管理列表
     *
     * @param preMuDifferentiationPolicy 差别化政策管理
     * @return 差别化政策管理
     */
    @Override
    public List<PreMuDifferentiationPolicy> selectPreMuDifferentiationPolicyList(PreMuDifferentiationPolicy preMuDifferentiationPolicy)
    {
        return preMuDifferentiationPolicyMapper.selectPreMuDifferentiationPolicyList(preMuDifferentiationPolicy);
    }

    /**
     * 新增差别化政策管理
     *
     * @param preMuDifferentiationPolicy 差别化政策管理
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertPreMuDifferentiationPolicy(PreMuDifferentiationPolicy preMuDifferentiationPolicy)
    {

        List<PreMuDifferentiationPolicyItem> items = preMuDifferentiationPolicy.getItems();
        if(items == null || items.isEmpty()){
            throw new RuntimeException("请填写政策条款");
        }
        preMuDifferentiationPolicy.setBaseCreateBy(SecurityUtils.getUserId());
        preMuDifferentiationPolicy.setBaseUpdateBy(SecurityUtils.getUserId());
        preMuDifferentiationPolicy.setBaseCreateTime(DateUtils.getNowDate());
        preMuDifferentiationPolicy.setBaseUpdateTime(DateUtils.getNowDate());
        preMuDifferentiationPolicy.setBaseId(IdUtil.simpleUUID());

        if(StringUtils.isNotBlank(preMuDifferentiationPolicy.getOriginalFilePath())){
            String originalFilePath = preMuDifferentiationPolicy.getOriginalFilePath();
            String replace = originalFilePath.replace(filePre,"");
            preMuDifferentiationPolicy.setOriginalFilePath("/file" + replace);
        }

        batchInsertPolicyItem(items,preMuDifferentiationPolicy.getBaseId());

        return preMuDifferentiationPolicyMapper.insertPreMuDifferentiationPolicy(preMuDifferentiationPolicy);
    }

    private void batchInsertPolicyItem(List<PreMuDifferentiationPolicyItem> items,String baseId){
        for(PreMuDifferentiationPolicyItem temp : items){
            temp.setBaseId(IdUtil.simpleUUID());
            temp.setBaseCreateBy(SecurityUtils.getUserId());
            temp.setBaseUpdateBy(SecurityUtils.getUserId());
            temp.setBaseCreateTime(DateUtils.getNowDate());
            temp.setBaseUpdateTime(DateUtils.getNowDate());
            temp.setPkId(baseId);
        }

        preMuDifferentiationPolicyItemMapper.batchInsertPolicyItem(items);
    }

    /**
     * 修改差别化政策管理
     *
     * @param preMuDifferentiationPolicy 差别化政策管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePreMuDifferentiationPolicy(PreMuDifferentiationPolicy preMuDifferentiationPolicy)
    {
        List<PreMuDifferentiationPolicyItem> items = preMuDifferentiationPolicy.getItems();
        if(items == null || items.isEmpty()){
            throw new RuntimeException("请填写政策条款");
        }
        //删除所有的条款
        preMuDifferentiationPolicyItemMapper.deletePreMuDifferentiationPolicyItemByPkId(preMuDifferentiationPolicy.getBaseId());

        if(StringUtils.isNotBlank(preMuDifferentiationPolicy.getOriginalFilePath())){
            String originalFilePath = preMuDifferentiationPolicy.getOriginalFilePath();
            String replace = originalFilePath.replaceAll(filePre,"");
            preMuDifferentiationPolicy.setOriginalFilePath("/file" + replace);
        }

        //插入新的条款
        batchInsertPolicyItem(items,preMuDifferentiationPolicy.getBaseId());
        return preMuDifferentiationPolicyMapper.updatePreMuDifferentiationPolicy(preMuDifferentiationPolicy);
    }

    /**
     * 批量删除差别化政策管理
     *
     * @param baseIds 需要删除的差别化政策管理主键
     * @return 结果
     */
    @Override
    public int deletePreMuDifferentiationPolicyByBaseIds(String[] baseIds)
    {
        return preMuDifferentiationPolicyMapper.deletePreMuDifferentiationPolicyByBaseIds(baseIds);
    }

    /**
     * 删除差别化政策管理信息
     *
     * @param baseId 差别化政策管理主键
     * @return 结果
     */
    @Override
    public int deletePreMuDifferentiationPolicyByBaseId(String baseId)
    {
        return preMuDifferentiationPolicyMapper.deletePreMuDifferentiationPolicyByBaseId(baseId);
    }
}
